package splug.cjk;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import splug.cjk.packet.FriendPacket;
import splug.cjk.packet.MessagePacket;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MessageTab1 extends ListActivity implements OnItemClickListener{

	private String id;
	private ArrayList<Content> content = new ArrayList<Content>();
	private ArrayList<MessageContent> sub_content = new ArrayList<MessageContent>();
	private ContentAdapter m_adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_tab1);

		Bundle b = this.getIntent().getExtras();
		id = b.getString("ID");

		ListView lv = getListView();

		//쪽지 목록을 받아온다
		try {
			Socket socket = new Socket(LetsGoActivity.server_ip, 2007);

			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();

			OutputStreamWriter osw = new OutputStreamWriter(os);
			InputStreamReader isr = new InputStreamReader(is);

			osw.write(17);
			osw.flush();

			int ack = isr.read();

			if(ack == '1'){
				/**
				 * 나의 아이디를 전송해준다
				 */
				MessagePacket packet = new MessagePacket(0, id, "", null, null, "", "");
				ObjectOutputStream output = new ObjectOutputStream(os);
				output.writeObject(packet);

				ObjectInputStream input = new ObjectInputStream(is);
				packet = (MessagePacket) input.readObject();
				
				/**
				 * 내 아이디에 해당하는 쪽지들을 받아온다
				 */
				content = packet.getContent();
				sub_content = packet.getSub_content();
			}
			else{
				Toast.makeText(MessageTab1.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
				finish();
			}


		} catch (UnknownHostException e) {
			Toast.makeText(MessageTab1.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
			finish();
		} catch (IOException e) {
			Toast.makeText(MessageTab1.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
			finish();
		} catch (ClassNotFoundException e) {
			Toast.makeText(MessageTab1.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
			finish();
		}

		m_adapter = new ContentAdapter(this, R.layout.list_property, content);
		lv.setAdapter(m_adapter);
	}

	private class ContentAdapter extends ArrayAdapter<Content> {

		private ArrayList<splug.cjk.Content> items;

		public ContentAdapter(Context context, int textViewResourceId, ArrayList<Content> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.list_property, null);
			}
			Content p = items.get(position);
			if (p != null) {
				TextView tt = (TextView) v.findViewById(R.id.toptext);
				TextView bt = (TextView) v.findViewById(R.id.bottomtext);
				if (tt != null){
					tt.setText(p.getTitle());
				}
				if(bt != null){
					bt.setText("보낸 사람: "+ p.getId());
				}
			}
			return v;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}	

	/**
	 * 리스트의 내용이 클릭되었을 때의 이벤트
	 * 새로운 액티비티가 생성된다
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long aa) {
		super.onListItemClick(l, v, position, aa);

		Intent intent = new Intent(MessageTab1.this, MessageTab1Content.class);
		Bundle b = new Bundle();
		b.putString("ID", id);
		intent.putExtras(b);
		b.putString("CONTENT", sub_content.get(position).getContent());
		intent.putExtras(b);
		b.putString("SENDER",sub_content.get(position).getId());
		intent.putExtras(b);
		b.putString("DATE", sub_content.get(position).getDate());
		intent.putExtras(b);
		startActivity(intent);
	}

}
